/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import model.Veiculos;
import dao.VeiculosDao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import util.HibernateUtil;

/**
 *
 * @author rafael
 */
public class VeiculosDaoImpl implements VeiculosDao {

    @Override
    public void inserir(Veiculos veiculos) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {

            session.beginTransaction();
            session.save(veiculos);
            session.beginTransaction().commit();
            GrowMessages.infoMessage(veiculos.getNome(), "Inserido com Sucesso");
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            GrowMessages.errorMessage(veiculos.getNome(), "Erro ao inserir");            
            System.out.println(e.getLocalizedMessage());
        } finally {
            session.close();
        }
    }

    @Override
    public void atualizar(Veiculos veiculos) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {

            session.beginTransaction();
            session.update(veiculos);
            session.beginTransaction().commit();
            GrowMessages.infoMessage(veiculos.getNome(), "Atualizado com Sucesso");

        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            GrowMessages.errorMessage(veiculos.getNome(), "Erro ao Atualizar");
            System.out.println(e.getLocalizedMessage());
        } finally {
            session.close();
        }
    }

    @Override
    public Integer selectMax() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria criteria = session.createCriteria(Veiculos.class);
            criteria.setProjection(Projections.max("pkVeiculos"));
            return (Integer) criteria.uniqueResult();
        } catch (Exception e) {
            System.out.println("Erro em MAX" + e.getLocalizedMessage());
            return 1;
        } finally {
            session.close();
        }
    }

    @Override
    public void eliminar(Veiculos veiculos) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.delete(veiculos);
            session.beginTransaction().commit();
            GrowMessages.infoMessage(veiculos.getNome(), "Eliminado com Sucesso");
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            GrowMessages.errorMessage(veiculos.getNome(), "Erro ao Eliminar");
            System.out.println(e.getLocalizedMessage());

        } finally {
            session.close();
        }
    }

    @Override
    public List<Veiculos> selectAll() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<Veiculos> lista = null;

        try {
            lista = session.createQuery("from Veiculos").list();
            return lista;
        } catch (HibernateException hibernateException) {
            System.out.println(hibernateException.getLocalizedMessage());
            return lista;
        } finally {
            session.close();
        }
    }

    @Override
    public List<Veiculos> selectByUser(int FK_VEICULOS_USUARIOS) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Veiculos.class);
        criteria.add(Restrictions.eq("usuarios.pkId", FK_VEICULOS_USUARIOS));
        List<Veiculos> lista = criteria.list();
        try {            
            return lista;
        } catch (HibernateException hibernateException) {
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }

    @Override
    public List<Veiculos> search(String search) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Veiculos.class);
        criteria.add(Restrictions.ilike("nome", search, MatchMode.START));
        List<Veiculos> lista = criteria.list();
        try {
            return lista;
        } catch (HibernateException hibernateException) {
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }
}
